<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">


            <el-form-item label="是否缴费" prop="status">
                <el-select v-model="queryParams.status" placeholder="缴费状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_expenses_master_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="Lists" >
            <el-table-column prop="community_id" label="所属片区">
                <template slot-scope="{row}">
                    {{row.community_info?row.community_info.title:''}}
                </template>
            </el-table-column>
            <el-table-column prop="apparatus_id" label="所属水机">
                <template slot-scope="{row}">
                    {{ row.water_device_info? row.water_device_info.water_title : "" }}
                </template>
            </el-table-column>
            <el-table-column label="费用" align="center" prop="amount_total" />
            <el-table-column prop="start_day" label="开始时间"/>
            <el-table-column prop="end_day" label="结束时间"/>
            <el-table-column prop="read_time" label="缴费时间"/>
            <el-table-column prop="status" align="center"  label="状态" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_expenses_master_status" :value="row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="created_time" width="200"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="{row}">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(row)"
                    >修改</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />
        <!-- 添加或修改部门对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="所属区片：" prop="community_id">
                    <el-select v-model="form.community_id"  @change="changeMachine" filterable  placeholder="请选择区片" size="small"  >
                        <el-option v-for="(item, index) in communityList" :key="index" :label="item.title" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属水机：" prop="water_id">
                    <el-select v-model="form.water_id"  placeholder="请选择水机" filterable size="small"   >
                        <el-option v-for="(item, index) in apparatusList" :key="index" :label="item.water_title +'('+item.code+')'" :value="item.water_id"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="本期时间" prop="status_end_day">
                    <el-date-picker v-model="form.status_end_month" style="width: 240px"   type="monthrange" range-separator="-"
                                    start-placeholder="开始月份"
                                    end-placeholder="结束月份"/>
                </el-form-item>
                <el-form-item label="缴费时间" prop="read_time">
                    <el-date-picker v-model="form.read_time" style="width: 240px" value-format="yyyy-MM-dd" type="date" />
                </el-form-item>
                <el-form-item label="物业费金额" prop="amount_total">
                    <el-input v-model="form.amount_total" placeholder="请输入物业费金额" />
                </el-form-item>

                <el-form-item label="费用周期" prop="time_type">
                    <el-radio-group v-model="form.time_type">
                        <el-radio
                            v-for="dict in time_type"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_expenses_master_status"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import { getAllCom} from "@/api/grzq/community.js";
import {
    getApparatusAllList,
} from "@/api/grzq/apparatus.js";


import {getSiteSelectList} from "@/api/system/site";


import {getPropertyList,getPropertyDetail,createProperty,updateProperty,propertyDetail,changeMasterReadFiledStatus,delProperty} from "@/api/expenses/property";


export default {
    name: "master_read",
    dicts:['sys_expenses_master_type','sys_expenses_master_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            Lists: [],
            communityList: [],
            apparatusList: [],
            masterList: [],
            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                type: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            time_type:[
                {
                    value:'1',
                    label:"月"
                },
                {
                    value:'3',
                    label:"季度"
                },
                {
                    value:'12',
                    label:"年"
                },
            ],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                community_id: [
                    { required: true, message: "请选择所属片区", trigger: "blur" }
                ],
                apparatus_id: [
                    { required: true, message: "请选择所属水机", trigger: "blur" }
                ],
                status_end_month: [
                    { required: true, message: "请选择月份", trigger: "blur" }
                ],
                read_time: [
                    { required: true, message: "抄表时间不能为空", trigger: "blur" }
                ],
                amount_total: [
                    { required: true, message: "请输入物业费用", trigger: "blur" }
                ],
                time_type: [
                    { required: true, message: "请选择费用周期", trigger: "blur" }
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getPropertyList(this.queryParams);
            that.Lists = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },
        //片区
        async getAllCom() {
            this.communityList=[];
            const res = await getAllCom();
            this.communityList = res.data;

        },
        //片区-水机
        async changeMachine(community_id) {
            this.apparatusList=[];
            const res = await getApparatusAllList({'community_id':community_id});
            this.apparatusList = res.data;
        },
        async getAllSite() {
            const res = await getSiteSelectList();
            this.siteList = res.data;
            console.log("this.siteList",this.siteList)
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                property_id: undefined,
                apparatus_id: undefined,
                site_id: undefined,
                status_end_month: undefined,
                remark: undefined,
                amount_total: undefined,
                time_type: "1",
                type: "property",
                status: "0"
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.getAllCom();
            this.open = true;
            this.title = "添加-物业费";
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            getPropertyDetail({property_id:row.property_id}).then(response => {
                let data= response.data;
                this.getAllCom();
                this.changeMachine(data.community_id);
                this.form = data
                this.form.status_end_day = [data.start_day, data.end_day]
                this.form.time_type = data.time_type.toString()
                this.form.status = data.status.toString()
                this.title = "编辑-物业费";
                this.open = true;
            })

        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.property_id != undefined) {
                        updateProperty(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        createProperty(this.form).then(response => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            // this.confirm('是否确认删除名称为"' + row.deptName + '"的数据项？').then(function() {
            //     return delMasterRead({property_id:row.property_id});
            // }).then(() => {
            //     this.getList();
            //     this.msgSuccess("删除成功");
            // }).catch(() => {});
        }
    }
};
</script>
